﻿using System;
using System.Xml.Linq;

using Game;

using Engine;

namespace ApiToolkit
{
	public class BooleanPropertyEditor : CanvasWidget
	{
		bool value;
		ButtonWidget ApplyButton;
		ButtonWidget ValueButton;
		
		PropertyEditor_Common Editor;
		public BooleanPropertyEditor(int content, string targetProperty, ContainerWidget parent)
		{
			try
			{
				this.Editor = new PropertyEditor_Common(content, targetProperty);
				XElement xelement = ContentManager.Get<XElement>("Widgets/BooleanPropertyEditor");
				LoadContents(this, xelement);
				
				this.ValueButton = Children.Find<ButtonWidget>("ValueButton");
				this.ApplyButton = Children.Find<ButtonWidget>("ApplyButton");
				this.value = (bool)Editor.TargetPropertyValue;
				this.ValueButton.Text = value.ToString();
			}
			catch (Exception ex)
			{
				Log.Error(ex.ToString());
				throw;
			}
		}
		public override void Update()
		{
			if(ApplyButton.IsClicked)
			{
				Apply();
			}
			if(ValueButton.IsClicked)
			{
				ChangeValue();
				ValueButton.Text = value.ToString();
			}
		}
		private void Apply()
		{
			Editor.TargetPropertyValue = value;
		}
		private void ChangeValue()
		{
			value = !value;
		}
	}
}